package com.sunwayworld.basemodule.coredata.experimentaltemplate.service.impl;

import com.sunwayworld.basemodule.coredata.experimentaltemplate.bean.LimsExperimentalTemplateBean;
import com.sunwayworld.basemodule.coredata.experimentaltemplate.bean.LimsExperimentalTemplateTestBean;
import com.sunwayworld.basemodule.coredata.experimentaltemplate.bean.LimsExperimentalTemplateTestMethodBean;
import com.sunwayworld.basemodule.coredata.experimentaltemplate.dao.LimsExperimentalTemplateDao;
import com.sunwayworld.basemodule.coredata.experimentaltemplate.service.LimsExperimentalTemplateService;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Repository
@GikamBean
public class LimsExperimentalTemplateServiceImpl implements LimsExperimentalTemplateService {

    @Autowired
    private LimsExperimentalTemplateDao limsExperimentalTemplateDao;

    @Autowired
    @Lazy
    private LimsExperimentalTemplateTestServiceImpl etTestService;

    @Autowired
    @Lazy
    private LimsExperimentalTemplateTestMethodServiceImpl etMethodService;
    @Override
    @SuppressWarnings("unchecked")
    public LimsExperimentalTemplateDao getDao() {
        return limsExperimentalTemplateDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsExperimentalTemplateBean limsExperimentalTemplate = jsonWrapper.parseUnique(LimsExperimentalTemplateBean.class);
        limsExperimentalTemplate.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsExperimentalTemplate);
        return limsExperimentalTemplate.getId();
    }

    @Override
    @Transactional
    public void delete(RestJsonWrapperBean wrapper) {
        List<LimsExperimentalTemplateBean> parse = wrapper.parse(LimsExperimentalTemplateBean.class);
        if(CollectionUtils.isEmpty(parse)) return;
        // 级联删除模板工序 和 模板工序方法
        List<LimsExperimentalTemplateTestBean> testBeans = new ArrayList<>();
        List<LimsExperimentalTemplateTestMethodBean> testMethodBeans = new ArrayList<>();
        parse.forEach(e -> {
            LimsExperimentalTemplateTestBean testBean = new LimsExperimentalTemplateTestBean();
            testBean.setEtId(e.getId());
            testBeans.add(testBean);
            LimsExperimentalTemplateTestMethodBean methodBean = new LimsExperimentalTemplateTestMethodBean();
            methodBean.setEtId(e.getId());
            testMethodBeans.add(methodBean);
        });
        etTestService.getDao().deleteBy(testBeans, "etId");
        etMethodService.getDao().deleteBy(testMethodBeans, "etId");
        getDao().deleteBy(parse);
    }
}
